//
//  IWTitleButton.m
//  8期微博
//
//  Created by apple on 14-9-1.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "TTTitleButton.h"
// 图片的宽度
#define IWTitleImageWidth 30

@implementation TTTitleButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 1.设置标题颜色
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        // 2.设置标题居中显示
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        // 3.设置图片居中显示
        self.imageView.contentMode = UIViewContentModeCenter;
    }
    return self;
}



- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleX = 0;
    CGFloat titleY = 0;
    CGFloat titleW = self.width - IWTitleImageWidth;
    CGFloat titleH = self.height;
    return CGRectMake(titleX, titleY, titleW, titleH);
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageX = self.width - IWTitleImageWidth;
    CGFloat imageY = 0;
    CGFloat imageH = self.height;
    CGFloat imageW = IWTitleImageWidth;
    return CGRectMake(imageX, imageY, imageW, imageH);
}


- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
    [super setTitle:title forState:state];
    
    // 根据设置的文本计算文本的按钮的frame
//    IWLog(@"title = %@", title);
    CGSize titleSize = [title sizeWithFont:self.titleLabel.font];
    self.width = titleSize.width + IWTitleImageWidth;
}
@end
